package com.sh.xxj.rebot.app;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.dom4j.DocumentException;

import com.sh.xxj.rebot.model.CourseModel;
import com.sh.xxj.rebot.model.CourseOptionModel;
import com.sh.xxj.rebot.model.TypeModel;
import com.sh.xxj.rebot.service.CourseService;

/**
 * 根据xml生成excel
 * 
 * @author Administrator
 * 
 */
public class GenerateExcelFromXml {

	/**
	 * @param args
	 * @throws DocumentException
	 * @throws IOException
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	public static void main(String[] args) throws DocumentException,
			IOException, RowsExceededException, WriteException {

		// 创建excel
		File fileExcel = new File("D:\\course.xls");
		WritableWorkbook wb = Workbook.createWorkbook(fileExcel);

		CourseService cs = new CourseService();
		List<TypeModel> listTypeModel = cs
				.getTypeModeFromFilePath("D:\\workspace_myeclipse10\\xxj_robot_web\\res\\data\\parent\\menu.xml");

		List<CourseOptionModel> listCOM = new ArrayList<CourseOptionModel>();

		HashMap<String, String> mapPics = new HashMap<String, String>();

		for (TypeModel tm : listTypeModel) {
			System.out
					.println("----------------------------------------------------");
			System.out.println("分类:" + tm.getName());
			WritableSheet wsheet = wb.createSheet(tm.getName(), 0);
			for (int i = 0; i < tm.getListCourseModel().size(); i++) {
				System.out.println("课程顺序:" + (i + 1));
				//写第一行
				wsheet.addCell(new Label(0, i, (i+1)+""));
				CourseModel cm = tm.getListCourseModel().get(i);
				for (int j = 0; j < cm.getListCourseOptionModel().size(); j++) {
					CourseOptionModel com = cm.getListCourseOptionModel()
							.get(j);
					wsheet.addCell(new Label(j+1,i,  com.getWord()));
					System.out.println("卡片:" + com.getWord());
				}//end for (int j = 0; j < cm.getListCourseOptionModel().size(); j++)
			}// end for (int i = 0; i < tm.getListCourseModel().size(); i++)
		}//end for (int i = 0; i < tm.getListCourseModel().size(); i++)
		
        // 主体内容生成结束        
		wb.write();
		wb.close();

	}

}
